<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '100px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    dialogFormVisible:false,
    disabled:false,
  })
  interface formQuert {
    nameplate_id:number
    nameplate_name:string
    nameplate_img:string
    nameplate_resource:string
    nameplate_desc:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    nameplate_name: [{ required: true, message: "请输入铭牌名称", trigger: "blur" }],
    nameplate_img: [{ required: true, message: "请上传铭牌图片", trigger: "blur" }],
    nameplate_resource: [{ required: true, message: "请上传铭牌动画", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      let submitSet = false
      const { nameplate_id } = form
      const loading = Toastloading()
      if(!nameplate_id) {
        await propApi.nameplateAdd(form).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await propApi.nameplateEdit(form).then(()=>{
          submitSet = true
          ToastSuccess('编辑成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    const { 
      id:nameplate_id=0, nameplate_name='',
      nameplate_img='', nameplate_resource='', nameplate_desc='',
    } = query
    Object.assign(form,{
      nameplate_id, nameplate_name, nameplate_img,
      nameplate_resource, nameplate_desc,
    })
    action.title = `${nameplate_id?'编辑铭牌：':'新增铭牌'}${nameplate_name}`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="60%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="铭牌名称" prop="nameplate_name">
          <el-input v-model="form.nameplate_name" placeholder="请输入铭牌名称"  />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="铭牌图片" prop="nameplate_img">
          <xmUpload
            v-model:file-url="form.nameplate_img"
            :data="{
              md5:1,
              type:29,
            }"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="铭牌动画" prop="nameplate_resource">
          <xmUpload
            v-model:file-url="form.nameplate_resource"
            :data="{
              md5:1,
              type:29,
            }"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="铭牌描述" prop="nameplate_desc">
          <el-input
            v-model="form.nameplate_desc"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请输入商品描述"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>